<?php

class MainPage extends BaseSitePage {

	function MainPage() {
		parent::BaseSitePage('MainPage');
			
		// alap content-ünk a főoldali cikk
		$this->defaultContentGeneratorInfo = new ContentGeneratorInfo("MainPageArticleGenerator", BaseContentGenerator::CONTEXT_PAGE_DEFAULT_CONTENT, "MainPage", null);
	}

	public function serveRequest() {
		parent::serveRequest();
		
		try {
			SiteBase::manageLoginLogout();
		} catch (Exception $e) {
			// akart lenni login, de nem sikerült!
			LoginBox::$loginError = $e->getMessage();
		}

		SiteBase::setExtraPopupIniParams(array("cssToAdd" => array("site/css/site.css", "site/css/article.css", "site/css/article_prof.css")));

		SiteBase::addCss(array("site/css/site.css", "site/css/article.css", "site/css/article_prof.css", 'site/css/kibicbridge.css', 'site/css/boxes.css'));
		SiteBase::addJavascript(array("js/enhancements.js", "site/js/siteScripts.js", 'js/menuStructure.js'));

		$tpl = BaseSitePage::loadTemplateFile("main.tpl.html");
		TextHandler::insertTextsIntoTemplate($tpl);
		$tpl->setVariable('app_root_url', APP_ROOT_URL);
		
		// beallitjuk js-nek a top menuhoz tartozo konstansokat
		$tpl->setVariable("topMenuMaxSubmenuWidth", TOP_MENU_MAX_SUBMENU_WIDTH);

		$currlang = TextHandler::getCurrentLangcode();

		// logo link - frontpage
		$frontPageUrl = ContentUrlManager::getSiteUrl(SiteHandler::getCurrentSiteId(), "full", TextHandler::getCurrentLangcode());
		if(is_null($frontPageUrl))
			$mainPageUrl = "#";
		$tpl->setVariable("frontPageUrl", $frontPageUrl);

		// lang selector cucc
		$langs = TextHandler::getEnabledLanguages();
		if (empty($langs)) {
			$tpl->replaceBlock('languages', '');
			$tpl->touchBlock('languages');
		} else {
			$langcodes = array_keys($langs);
			foreach ($langcodes as $langcode) {
				$tpl->setCurrentBlock('languages');
				$href = HydraLinkProvider::getLink('languageUrl', array("langcode" => $langcode));
				$tpl->setVariable('langcode', $langcode);
				$tpl->setVariable('link_language', $href);
				$tpl->setVariable('lang_alt', TextHandler::getText("lang_$langcode"));
				$tpl->parseCurrentBlock();
			}
		}

		// versenynaptár
		$tournamentMonths = SiteDBHelper::getTournamentMonths();
		for ($i = 1; $i <= 12; $i++) {
			$tpl->setCurrentBlock('tournament');
			if ($tournamentMonths[$i]) {
				$tpl->setVariable('tournament_class', 'holink');
				$tpl->setVariable('tournamentMonthLink', HydraLinkProvider::getLink('tournamentMonthLink', array('month' => $i, 'year' => date('Y'))));
			} else {
				$tpl->setVariable('tournament_class', 'holink_archiv');
				$tpl->setVariable('tournamentMonthLink', HydraLinkProvider::getLink('tournamentMonthLink', array('month' => $i, 'year' => date('Y'))));
			}
			$tpl->setVariable('tournament_month', TextHandler::getText("mon_$i"));
			if ($i == 12) $tpl->setVariable('divider', '');
			else $tpl->setVariable('divider', '|');
			$tpl->parseCurrentBlock();
		}

		$title = TextHandler::getText('page_title',$currlang);
		$tpl->setVariable('title', $title);
		
		// üzenetek!
		$msgs = FunctionLibrary::getSessionValue('error_messages');
		if(!empty($msgs)) {
			unset($_SESSION['error_messages']);
			$messages = "";
			foreach ($msgs as $msg) {
				$messages .= "<p>$msg</p>";
			}
			$tpl->setCurrentBlock('error_messages');
			$tpl->setVariable('Errors', $messages);
			$tpl->parseCurrentBlock();
		} else {
			$tpl->setVariable('errorsClass', 'hidden');
		}
		$msgs = FunctionLibrary::getSessionValue('warning_messages');
		if(!empty($msgs)) {
			
			unset($_SESSION['warning_messages']);
			$messages = "";
			foreach ($msgs as $msg) {
				$messages .= "<p>$msg</p>";
			}
			$tpl->setCurrentBlock('warning_messages');
			$tpl->setVariable('Warnings', $messages);
			$tpl->parseCurrentBlock();
		} else {
			$tpl->setVariable('warningsClass', 'hidden');
		}
		$msgs = FunctionLibrary::getSessionValue('success_messages');
		if(!empty($msgs)) {
			
			unset($_SESSION['success_messages']);
			$messages = "";
			foreach ($msgs as $msg) {
				$messages .= "<p>$msg</p>";
			}
			
			$tpl->setCurrentBlock('success_messages');
			$tpl->setVariable('Successes', $messages);
			$tpl->parseCurrentBlock();
		} else {
			$tpl->setVariable('successClass', 'hidden');
		}

		// és mehet bele a content ha tud!
		$urlDrivenContent = SiteBase::generateUrlDrivenContent();
		if(is_null($urlDrivenContent)) {
			if(SiteBase::$shouldDo404) {
				// hát, ez bukta...
				FunctionLibrary::setHttpStatus(404);
				$urlDrivenContent = SiteBase::getErrorMessageContent(TextHandler::getText("page_not_found_404"));
			} else {
				$urlDrivenContent = "";
			}
		}
		$tpl->setVariable('urlDrivenContent', $urlDrivenContent);
		
		///// menu generalasa /////	
		
		// felso_menu
		$mainMenuHtml = MenuGroup::generateMenuGroupHtml("felso_menu");
		if (is_null($mainMenuHtml)) {
			$mainMenuHtml = "";
		}
		$tpl->setVariable('mainMenuHtml', $mainMenuHtml);
		$topMenuJsStruct = MenuGroup::$menuGroups["felso_menu"]->getJavascriptRepresentation("topMenu");
		$tpl->setVariable("topMenuStructure", $topMenuJsStruct);

		// lablec menu
		$footerMenuHtml = MenuGroup::generateMenuGroupHtml("lablec_menu", array($this, "onFooterMenuGeneratesHtml"));
		if(is_null($footerMenuHtml))
			$footerMenuHtml = "";
		$tpl->setVariable('lablecMenuHtml', $footerMenuHtml);

		// ki kell talalni melyik menugroup aktiv
		$item = MenuGroup::$selectedMenuItem;
		if (!empty($item))
			$groupName = $item->parentMenuGroup->name;
		else
			$groupName = '';
			
		switch ($groupName) {			
			case 'felso_menu':
				$tpl->setVariable('activeMenuGroup', 'amature mainMenuActive');
				break;
			
			case 'lablec_menu':
				$tpl->setVariable('activeMenuGroup', 'footerMenuActive');
				break;
			default:
				$tpl->setVariable('activeMenuGroup', 'noMenuActive');
				break;
		}

		// dobozok
		$currLangcode = TextHandler::getCurrentLangcode();
		
		// fake login box
		$loginBoxObj = new LoginBox($currLangcode);
		BoxHandler::addBox($loginBoxObj, $currLangcode);

		// jelentkezés doboz, ha verseny oldalon vagyunk
		$tournamentPageInfo = SiteBase::getCurrentContentGeneratorInfo();
		if (! empty($tournamentPageInfo) && $tournamentPageInfo->contentGeneratorClassName == 'TournamentPageGenerator') {
			$trBoxObj = new TournamentRegistrationBox($currLangcode);
			BoxHandler::addBox($trBoxObj, $currLangcode);
		}

		// db dobozok
		BoxHandler::loadFromDB($currLangcode);
		$boxes = BoxHandler::getBoxes($currLangcode);
		$boxPlaceArray = array_keys(ConstHandler::get("BOX_PLACES"));
		if(!is_null($boxes)) {
			BoxHandler::initBoxesForArticle(SiteBase::$currentArticleId, $currLangcode);

			foreach($boxes as $placeId => $boxPlaces) {
				$boxesHtmls = array();
				foreach($boxPlaces as $boxObj) {
					array_push($boxesHtmls, $boxObj->generateHtml());
				}
				$tpl->setVariable("boxes_$placeId", implode("\n", $boxesHtmls));
				
				unset($boxPlaceArray[array_search($placeId, $boxPlaceArray)]);
			}
		}

		foreach ($boxPlaceArray as $placeId) {
			$tpl->setVariable("boxes_$placeId", "");
		}

		$tpl->setVariable("boxJavascriptRepresentation", BoxHandler::getBoxesJavascriptRepresentation($currLangcode));
		$tpl->setVariable('boxOpenIconUrl', BOX_OPEN_ICON);
		$tpl->setVariable('boxCloseIconUrl', BOX_CLOSE_ICON);

		$tpl->parseCurrentBlock();
			
		return $tpl->get();
	}
	
	/**
	 * A menü generálja a html-t
	 * 
	 * @param MenuItem $menuItem ez generál mostan
	 * @param HTML_Template_ITX $tpl a sablon, amivel dolgozik - ebbe rakhatunk extra dolgokat
	 */
	public function onFooterMenuGeneratesHtml($menuItem, $tpl) {
		$menuIds = array_keys($menuItem->parentMenuGroup->menuItems);
		$index = FunctionLibrary::arrayGetIndex($menuIds, $menuItem->id);
		if($index < count($menuIds) - 1)
			$tpl->setVariable("separator", "&#160;| ");
		else
			$tpl->setVariable("separator", "");
	}

}
?>